<template>
  <div class="app-container">
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="420px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="108px"
      >
        <el-col :span="24">
          <el-form-item label="物料代码" prop="itmWldm">
            <el-input
              v-model="queryParams.wldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="客户编码" prop="itmCustwldm">
            <el-input
              v-model="queryParams.custwldm"
              placeholder="请输入客户编码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="物料品名" prop="itmWlpm">
            <el-input
              v-model="queryParams.wlpm"
              placeholder="请输入物料品名"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="display: flex; margin-top: 20px; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['purchase:minPackage:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      highlight-current-row
      v-loading="loading"
      :data="minPackageList"
      border
      stripe
      :height="tableH"
      id="table-wrapper"
      class="infinite-list"
      @scrollBottom="load"
      :row-style="{ padding: '0px' }"
      :cell-style="{ padding: '0px' }"
      :header-row-style="{ padding: '2px' }"
      :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
    >
      <el-table-column label="#" type="index" width="55" align="center" />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        align="center"
        prop="wldm"
      />
      <el-table-column
        label="客户编码"
        :show-overflow-tooltip="true"
        align="center"
        prop="custwldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        prop="wlpm"
      />
      <el-table-column
        label="物料规格"
        :show-overflow-tooltip="true"
        prop="wlgg"
      />
      <el-table-column
        label="单位"
        :show-overflow-tooltip="true"
        width="60"
        align="center"
        prop="dw"
      >
        <template #default="scope">
          <dict-tag :options="unitFlagOptions" :value="scope.row.dw" />
        </template>
      </el-table-column>
      <el-table-column
        label="最小包装"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
      >
        <template #default="scope">
          <el-input class="inputName" v-model="scope.row.minbz"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="55"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            type="text"
            size="mini"
            @click="updateitmMinbz(scope.row)"
            v-hasPermi="['purchase:minPackage:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  listMinPackage,
  updateitmMinbz,
  exportItmMinbz,
} from "@/api/project/materiel";

export default {
  name: "MinPackage",
  data() {
    return {
      upLoading: true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      tableH: document.body.clientHeight - 300,
      //存储当前厂商名
      csmNames: [],

      //保存当前厂商属性
      csmData: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 最小包装List
      minPackageList: [],
      tableData: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openStr: false,
      // 有效标识字典
      unitFlagOptions: [],
      openSearch: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 40,
        wldm: null,
        custwldm: null,
        pmgg: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询供应商资料列表 */
    getList() {
      // this.loading = true
      this.queryParams.pageNum = 1;
      listMinPackage(this.queryParams).then((response) => {
        this.minPackageList = [];
        this.minPackageList = this.minPackageList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    load() {
      // this.$message.success("加载下一页");
      this.queryParams.pageSize = 40;
      this.queryParams.pageNum++;
      listMinPackage(this.queryParams).then((response) => {
        this.minPackageList = this.minPackageList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.openSearch = false;
    },
    handleSearch() {
      this.reset();
      this.title = "查询";
      this.openSearch = true;
    },
    // 表单重置
    reset() {
      this.form = {
        custwldm: null,
        dw: null,
        minbz: null,
        wldm: null,
        wlgg: null,
        wlpm: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.minPackageList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改 */
    updateitmMinbz(row) {
      const data = {};
      data.wldm = row.wldm;
      data.minbz = row.minbz;
      this.$confirm(
        '是否确认修改物料代码: <"' + row.wldm + '"> 的最小包装?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          return updateitmMinbz(data);
        })
        .then(() => {
          // this.queryParams.pageNum = 1
          // this.minPackageList = [];
          this.getList();
          this.msgSuccess(row.wldm + " 最小包装修改成功~");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有物料最小包装数据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportItmMinbz(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.inputName > .el-input__inner {
  width: 80px;
  height: 20px;
  line-height: 20px;
}
</style>
